/**********************************************************************
 *
 * Filename:    supplicant.c
 * 
 * Description: the supplicant mechanism.
 *
 * Notes:
 *		v0.2:
 *			1. add authIP member of supplicant struct
 *			2. delete the ip/netmask/netgate/netaddr/dns member of supplicant struct
 *			3. fix lower case bug in the loadconfig.
 *		
 *
 **********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "supplicant.h"
#include "log.h"


struct client {
	char *name;
	int (*run)(supplicant_t *this, int version);
	int version;
};

extern int client_cernet(supplicant_t *this, int version);
extern int client_ruijie(supplicant_t *this, int version);


static struct client supplicant_client[] = {
	{"cernet",		client_cernet,			0},
	{"ruijie",		client_ruijie,			0x031e},
	{"ruijie3.30",	client_ruijie,			0x031e},
	{"ruijie3.22",	client_ruijie,			0x0316},
	{"ruijie3.13",	client_ruijie,			0x030d},
	{"ruijie3.05",	client_ruijie,			0x0305},
	{"ruijie2.56",	client_ruijie,			0x0238},
	{"ruijie2.43",	client_ruijie,			0x022b},
	{"ruijie2.41",	client_ruijie,			0x0229},
	{"ruijie2.31",	client_ruijie,			0x021f},
	{NULL, 			NULL, 				0}
};

supplicant_t * supplicant_create()
{
	extern char *client_name, *nic, *username, *password, *ip, *netmask, *netgate, *dns, *broadcast_mac;
	extern int echo_interval, auto_reconnect, auth_timeout, server_timeout;
	supplicant_t *this;
	this = (supplicant_t *)malloc(sizeof(supplicant_t));
	assert(this);

	// initialize the base supplicant infomation:
	strcpy(this->client_name, client_name);
	strcpy(this->nic, nic);
	strcpy(this->username, username);
	strcpy(this->password, password);
	this->ip=ip_pton(ip);
	this->netmask=ip_pton(netmask);
	this->netgate=ip_pton(netgate);
	this->dns=ip_pton(dns);
	memcpy(this->broadcastMAC, mac_pton(broadcast_mac), 6);
	this->echoInterval=echo_interval;
	this->autoReconnect=auto_reconnect;
	this->authenticatorTimeout=auth_timeout;
	this->serverTimeout=server_timeout;

	return this;
}
void supplicant_destroy(supplicant_t *this)
{
	assert(this);
	free(this);
}

int supplicant_run(supplicant_t *this)
{
	struct client *client;
	for(client=supplicant_client; client->name!=NULL; client++){
		if(strcmp(client->name, this->client_name)==0){
			break;
		}
	}
	if(!client->name){
		fprintf(stderr, "ERROR: %s not found!\n", this->client_name);
		return -1;
	}
	return client->run(this, client->version);
}

int supplicant_show(supplicant_t *this)
{
	assert(this);
	LOG(INFO, "client_name=%s\n", this->client_name);
	LOG(INFO, "nic=%s\n", this->nic);
   	LOG(INFO, "username=%s\n", this->username);
	LOG(INFO, "password=%s\n", this->password);
	LOG(INFO, "authIP=%s\n", ip_ntop(this->ip));
	LOG(INFO, "broadcastMAC=%s\n", mac_ntop(this->broadcastMAC));
	LOG(INFO, "echoInterval=%d\n", this->echoInterval);
	LOG(INFO, "autoReconnect=%d\n",this->autoReconnect);
	LOG(INFO, "authenticatorTimeout=%d\n", this->authenticatorTimeout);
	LOG(INFO, "serverTimeout=%d\n", this->serverTimeout);	
	return 0;
}

